// pages/index/ranking/ranking.js
import { scrollToLeftEnd } from '../../../utils/behavior';
const app = getApp();
Page({
    behaviors: [scrollToLeftEnd],

    /**
     * 页面的初始数据
     */
    data: {
        lineLeft: [84, 334, 584],
        current: 1,
        userInfoArr: [0, 0, 0],
        // 国家
        userArrCountry: [],
        pageNumCountry: 0,
        loadingCountry: false,
        loadingAllCountry: false,
        // 城市
        userArrCity: [],
        pageNumCity: 0,
        loadingCity: false,
        loadingAllCity: false,
        // 打卡数
        userArrCheck: [],
        pageNumCheck: 0,
        loadingCheck: false,
        loadingAllCheck: false,
    },
    // 查看打卡国家数\城市数\打卡数
    userRankListTap(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url,
        });
    },
    // 本人信息跳转
    userTap(e) {
        let url = '';
        let _that = this;
        if (_that.data.current == 0) {
            url = `./countryCheckInfo/countryCheckInfo?checkincountrynum=${e.currentTarget.dataset.num}&checkincountryname=${e.currentTarget.dataset.name}&uid=${e.currentTarget.dataset.uid}`;
        } else if (_that.data.current == 1) {
            url = `./cityCheckInfo/cityCheckInfo?checkincitynum=${e.currentTarget.dataset.num}&checkincityname=${e.currentTarget.dataset.name}&uid=${e.currentTarget.dataset.uid}`;
        } else {
            url = `../../minePage/checkNumber/checkNumber?checkinnum=${e.currentTarget.dataset.num}&checkinname=${e.currentTarget.dataset.name}&uid=${e.currentTarget.dataset.uid}`;
        }
        wx.navigateTo({
            url: url,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCheckinList();
        this.getCityList();
        this.getCountryList();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    changeCurrent(e) {
        this.setData({
            current: e.detail.current,
        });
    },
    changeswiperCurrent(e) {
        this.setData({
            current: e.currentTarget.dataset.index,
        });
    },
    catchTouchMove: function (res) {
        return false;
    },
    // 国家数
    getCountryList() {
        let _that = this;
        if (!_that.data.loadingAllCountry) {
            _that.setData({
                pageNumCountry: _that.data.pageNumCountry + 1,
                loadingCountry: true,
                loadingAllCountry: false,
            });
            wx.request({
                url:
                    app.globalData.rootPath + `cjMap/getCheckinCountryRankList`,
                method: 'GET',
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                },
                data: {
                    authId: app.globalData.authId,
                    pageSize: 10,
                    pageNo: _that.data.pageNumCountry,
                },
                success: function (res) {
                    if (res.data.status == 0) {
                        // 本人信息
                        let userInfo = {};
                        userInfo.rank = res.data.personalCheckinInfo.rank;
                        userInfo.realName = res.data.personalInfo.realname;
                        userInfo.realImg = res.data.personalInfo.realnapicurl;
                        userInfo.num =
                            res.data.personalCheckinInfo.checkincountrynum;
                        userInfo.num =
                            res.data.personalCheckinInfo.checkincountrynum;
                        userInfo.cityTeam = res.data.personalInfo.localcity;
                        userInfo.uid = res.data.personalInfo.uid;
                        _that.data.userInfoArr.splice(0, 1, userInfo);
                        // 排名列表
                        if (res.data.data && res.data.data.length) {
                            for (let i = 0; i < res.data.data.length; i++) {
                                _that.data.userArrCountry.push(
                                    res.data.data[i]
                                );
                            }
                        }
                        _that.setData({
                            userArrCountry: _that.data.userArrCountry,
                            userInfoArr: _that.data.userInfoArr,
                            loadingCountry: false,
                        });
                        if (res.data.data.length < 10) {
                            _that.setData({
                                loadingAllCountry: true,
                            });
                        }
                    } else {
                        if (res.data.errmsg == '输入页码超出范围') {
                            _that.setData({
                                loadingAllCountry: true,
                                loadingCountry: false,
                            });
                        }
                    }
                },
            });
        }
    },
    // 城市数
    getCityList() {
        let _that = this;
        if (!_that.data.loadingAllCity) {
            _that.setData({
                pageNumCity: _that.data.pageNumCity + 1,
                loadingCity: true,
                loadingAllCity: false,
            });
            wx.request({
                url: app.globalData.rootPath + `cjMap/getCheckinCityRankList`,
                method: 'GET',
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                },
                data: {
                    authId: app.globalData.authId,
                    pageSize: 10,
                    pageNo: _that.data.pageNumCity,
                },
                success: function (res) {
                    if (res.data.status == 0) {
                        let userInfo = {};
                        userInfo.rank = res.data.personalCheckinInfo.rank;
                        userInfo.realName = res.data.personalInfo.realname;
                        userInfo.realImg = res.data.personalInfo.realnapicurl;
                        userInfo.num =
                            res.data.personalCheckinInfo.checkincitynum;
                        userInfo.cityTeam = res.data.personalInfo.localcity;
                        userInfo.uid = res.data.personalInfo.uid;
                        _that.data.userInfoArr.splice(1, 1, userInfo);
                        // 排名列表
                        if (res.data.data && res.data.data.length) {
                            for (let i = 0; i < res.data.data.length; i++) {
                                _that.data.userArrCity.push(res.data.data[i]);
                            }
                        }
                        _that.setData({
                            userArrCity: _that.data.userArrCity,
                            userInfoArr: _that.data.userInfoArr,
                            loadingCity: false,
                        });
                        if (res.data.data.length < 10) {
                            _that.setData({
                                loadingAllCity: true,
                            });
                        }
                    } else {
                        if (res.data.errmsg == '输入页码超出范围') {
                            _that.setData({
                                loadingAllCity: true,
                                loadingCity: false,
                            });
                        }
                    }
                },
            });
        }
    },
    // 打卡数
    getCheckinList() {
        let _that = this;
        if (!_that.data.loadingAllCheck) {
            _that.setData({
                pageNumCheck: _that.data.pageNumCheck + 1,
                loadingCheck: true,
                loadingAllCheck: false,
            });
            wx.request({
                url: app.globalData.rootPath + `cjMap/getCheckinRankList`,
                method: 'GET',
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                },
                data: {
                    authId: app.globalData.authId,
                    pageSize: 10,
                    pageNo: _that.data.pageNumCheck,
                },
                success: function (res) {
                    if (res.data.status == 0) {
                        let userInfo = {};
                        userInfo.rank = res.data.personalCheckinInfo.rank;
                        userInfo.realName = res.data.personalInfo.realname;
                        userInfo.realImg = res.data.personalInfo.realnapicurl;
                        userInfo.num = res.data.personalCheckinInfo.checkinnum;
                        userInfo.cityTeam = res.data.personalInfo.localcity;
                        userInfo.uid = res.data.personalInfo.uid;
                        _that.data.userInfoArr.splice(2, 1, userInfo);
                        // 排名列表
                        if (res.data.data && res.data.data.length) {
                            for (let i = 0; i < res.data.data.length; i++) {
                                _that.data.userArrCheck.push(res.data.data[i]);
                            }
                        }
                        _that.setData({
                            userArrCheck: _that.data.userArrCheck,
                            userInfoArr: _that.data.userInfoArr,
                            loadingCheck: false,
                        });
                        if (res.data.data.length < 10) {
                            _that.setData({
                                loadingAllCheck: true,
                            });
                        }
                    } else {
                        if (res.data.errmsg == '输入页码超出范围') {
                            _that.setData({
                                loadingAllCheck: true,
                                loadingCheck: false,
                            });
                        }
                    }
                },
            });
        }
    },
});
